package org.eclipse.football;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class DefPlayer {
	// Declare private member variables
	private String name;
	private String team;
	private int def_tds;
	private int picks;
	private int fumbles;
	private int sacks;
	private int blocks;
	private int safeties;
	private int pa;
	private int ya;
	
	// Define constructor
	public DefPlayer(String name, String team){
		// Set name and team based off input
		this.name = name;
		this.team = team;
		
		// Initialize all other member variables to 0
		def_tds = 0;
		picks = 0;
		fumbles = 0;
		sacks = 0;
		blocks = 0;
		safeties = 0;
		pa = 0;
		ya = 0;
	}

	// Define function to calculate points for this player
	public int calculatePoints(){
		// Initialize sum to 0
		int sum = 0;
		
		//TODO: JUST A MARKER FOR THIS MALARKY
		sum = updatePerformanceFromFile(18);
		
		// Add points for tds
		sum += 6*def_tds;
		
		// Add points for turnovers, safeties, and blocked kicks
		sum += 2*(picks+fumbles+blocks+safeties);
		
		// Add points for sacks
		sum += sacks;
		
		// Add points for yards allowed based off range
		if(ya < 100){
			sum += 5;
		}
		else if(ya < 200){
			sum += 3;
		}
		else if(ya < 300){
			sum += 2;
		}
		else if(ya < 350){
			// Add 0 pts
		}
		else if(ya < 400){
			sum -= 1;
		}
		else if(ya < 450){
			sum -= 3;
		}
//		else if(ya < 500){
//			sum -= 5;
//		}
//		else if(ya < 550){
//			sum -= 6;
//		}
		else{
			sum -= 7;
		}
		
		// Return sum
		return sum;
	}

	// Getters and setters
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTeam() {
		return team;
	}
	
	public void setTeam(String team) {
		this.team = team;
	}
	
	public int getDef_tds() {
		return def_tds;
	}

	public void setDef_tds(int def_tds) {
		this.def_tds = def_tds;
	}

	public int getPicks() {
		return picks;
	}

	public void setPicks(int picks) {
		this.picks = picks;
	}

	public int getFumbles() {
		return fumbles;
	}

	public void setFumbles(int fumbles) {
		this.fumbles = fumbles;
	}

	public int getSacks() {
		return sacks;
	}

	public void setSacks(int sacks) {
		this.sacks = sacks;
	}

	public int getBlocks() {
		return blocks;
	}

	public void setBlocks(int blocks) {
		this.blocks = blocks;
	}

	public int getSafeties() {
		return safeties;
	}

	public void setSafeties(int safeties) {
		this.safeties = safeties;
	}

	public int getPa() {
		return pa;
	}

	public void setPa(int pa) {
		this.pa = pa;
	}

	public int getYa() {
		return ya;
	}

	public void setYa(int ya) {
		this.ya = ya;
	}

	// Define function to check the specified CSV for this player's
	// actual game performance
	public int updatePerformanceFromFile(Integer week){
		int sum = 0;
		ArrayList<File> ourFiles = new ArrayList<File>();
		File defensive;
		FileReader inputFileReader = null;
		defensive = new File("defclean.txt");
		if ( defensive.exists()) {
			ourFiles.add(defensive);
		} else {
			System.err.println("defclean.txt not found");
		}
		pa = 0;
		int weekpa = 0;
		String theCurrentLine;
		ArrayList<String> theCurrentLineSplit;
		BufferedReader fr = null;
		Scanner sc = null;
		boolean found = false;
		for (File fileToCheck:ourFiles){
			try 
			{
				//Attach to file
				fr = new BufferedReader(new FileReader(fileToCheck));	
				//Read from file only if ready to be read
				while (fr.ready()){
					//Read line as String
					theCurrentLine= fr.readLine();
					//Split Line into parts to find the name at the front
					theCurrentLineSplit = new ArrayList<String>(Arrays.asList(theCurrentLine.split(",")));
					//Check the name at the start of the line to match this player
					if (theCurrentLine.contains(name)){
						//Scanner == String Parser
						sc = new Scanner(theCurrentLine).useDelimiter(",");
						//EXAMPLE LINE:
				/*
				:Week 1
				189310908009,Green Bay,42, New Orleans,34
				137310912007,Oakland,23, Denver,20
				:Week 2
				128310918008,Detroit,48, Kansas City,3
				*/
						if (theCurrentLineSplit.get(1).equalsIgnoreCase(name)){
							sc.next();
							sc.next();
							sc.next();
							sc.next();
							weekpa = sc.nextInt();
							pa += weekpa;
						} else {
							sc.next();
							sc.next();
							weekpa = sc.nextInt();
							pa += weekpa;
						}
//						 Add points for points allowed based off range
						if(weekpa == 0){
							sum += 5;
						}
						else if(weekpa <= 6){
							sum += 4;
						}
						else if(weekpa <= 13){
							sum += 3;
						}
						else if(weekpa <= 17){
							sum += 1;
						}
						else if(weekpa <= 27){
							// Add 0 pts
						}
						else if(weekpa <= 34){
							sum -= 1;
						}
						else if(weekpa <= 45){
							sum -= 3;
						}
						else{
							sum -= 5;
						}
					}
				}
			}
			catch (IOException ioe)
			{
				ioe.printStackTrace();
			}
			finally
			{
				if (fr != null && sc != null)
				{
					try
					{
						fr.close();
						sc.close();
					}
					catch (IOException ioe)
					{
						ioe.printStackTrace();
					}
				}
			}
		}
		return sum;
	}
}
